<template>
  <div>
    <el-table
      ref="singleTable"
      :data="skuList"
      highlight-current-row
      style="width: 100%"
      stripe
      border
    >
      <el-table-column type="index" width="50" label="序号"> </el-table-column>
      <el-table-column property="skuName" label="名称" width="200">
      </el-table-column>
      <el-table-column property="skuDesc" label="描述" width="200">
      </el-table-column>
      <el-table-column label="默认图片" width="120">
        <template slot-scope="{ row }">
          <div>
            <el-image
              style="width: 100px; height: 100px"
              :src="row.skuDefaultImg"
            ></el-image>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="重量(KG)"
        property="weight"
        style="text-align: center"
        >1kg</el-table-column
      >
      <el-table-column label="价格(元)" property="price" width="100">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="{ row }">
          <div style="margin-left: 10px">
            <el-button
              v-if="row.isSale == 0"
              title="上架"
              type="info"
              size="mini"
              icon="el-icon-top"
              @click="upOrDown(row.id, true)"
            />
            <el-button
              v-if="row.isSale == 1"
              title="下架"
              type="success"
              size="mini"
              icon="el-icon-bottom"
              @click="upOrDown(row.id, false)"
            />
            <el-button
              title="修改"
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="changeInfo"
            />
            <el-button
              title="查看详情"
              type="info"
              size="mini"
              icon="el-icon-info"
              @click="showSkuInfo(row.id)"
            />
            <el-popconfirm
              :title="`确定删除 ${row.skuName} 吗`"
              @onConfirm="deleteSku(row.id)"
            >
              <el-button
                style="margin-left: 10px"
                slot="reference"
                type="danger"
                size="mini"
                icon="el-icon-delete"
                title="删除"
              ></el-button>
            </el-popconfirm>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[5, 10, 20, 30, 40, 50]"
      style="padding: 20px 0; text-align: center"
      layout="prev, pager, next, jumper, ->, sizes, total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!--  抽屉  -->
    <el-drawer :visible.sync="isShowSkuInfo" :with-header="false" size="50%">
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{ skuInfo.price }}</el-col>
      </el-row>

      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="18">
          <el-tag
            type="success"
            style="margin-right: 5px"
            v-for="value in skuInfo.skuAttrValueList"
            :key="value.id"
          >
            {{ value.attrId + "-" + value.valueId }}
          </el-tag>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="5">销售属性</el-col>
        <el-col :span="18">
          <el-tag
            type="success"
            style="margin-right: 5px"
            v-for="value in skuInfo.skuSaleAttrValueList"
            :key="value.id"
          >
            {{ value.id + "-" + value.saleAttrValueId }}
          </el-tag>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="5">商品图片</el-col>
        <el-col :span="16">
          <el-carousel class="sku-carousel" trigger="click" height="400px">
            <el-carousel-item
              v-for="item in skuInfo.skuImageList"
              :key="item.id"
            >
              <img :src="item.imgUrl" />
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>
<script>
export default {
  name: "Sku",
  data() {
    return {
      skuList: [],
      total: 0,
      page: 1,
      limit: 5,
      isShow: true,
      isShowSkuInfo: false,
      skuInfo: {},
    };
  },
  mounted() {
    this.getSkuList();
  },
  methods: {
    //获取sku列表
    async getSkuList(page = 1) {
      this.page = page;
      const result = await this.$API.sku.getList(this.page, this.limit);
      this.skuList = result.data.records;
      this.total = result.data.total;
    },

    //切换每页显示几条
    async handleSizeChange(val) {
      const result = await this.$API.sku.getList(this.page, val);
      this.skuList = result.data.records;
      this.total = result.data.total;
    },
    //页码改变
    handleCurrentChange(val) {
      this.getSkuList(val);
    },
    //修改skuInfo
    changeInfo() {
      this.$message.info("暂未开发");
    },
    //上架和下架
    upOrDown(id, type) {
      //上架
      if (type) {
        this.$API.sku.onSale(id).then((result) => {
          this.$message({
            type: "success",
            message: "上架成功!",
          });
          this.getSkuList(this.page);
        });
      } else {
        //下架
        this.$API.sku.cancelSale(id).then((result) => {
          this.$message({
            type: "success",
            message: "下架成功!",
          });
          this.getSkuList(this.page);
        });
      }
    },
    //删除sku
    async deleteSku(id) {
      try {
        await this.$API.sku.remove(id);
        this.$message.success("删除SKU成功");
        this.getSkuList(1);
      } catch (error) {
        this.$message({
          message: error.message || "删除SKU失败",
          type: "error",
        });
      }
    },
    //查看详情
    async showSkuInfo(id) {
      this.isShowSkuInfo = true;
      const result = await this.$API.sku.get(id);
      this.skuInfo = result.data;
    },
  },
};
</script>
<style lang="scss" scoped>
.el-drawer__body {
  .el-row {
    height: 40px;
    .el-col {
      line-height: 40px;
      &.el-col-5 {
        font-size: 18px;
        font-weight: bold;
        text-align: right;
        margin-right: 20px;
      }
    }
  }
  .sku-carousel {
    width: 400px;
    border: 1px solid #ccc;
    img {
      width: 400px;
      height: 400px;
    }
    .el-carousel__indicator {
      button {
        background-color: hotpink;
      }
      &.is-active {
        button {
          background-color: green;
        }
      }
    }
  }
}
</style>